package com.xunshangwang.pinhaohuo.ui.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.xunshangwang.pinhaohuo.R;
import com.xunshangwang.pinhaohuo.ui.activities.BaseActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xzp on 16/2/26.
 */
public class MyGroupFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener{
    private RadioGroup mRadioGroup;
    private BaseFragment mCurrFragment;
    private Map<Integer,GroupListFragment> mGroupListMap;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_my_group,container,false);
        mRadioGroup= (RadioGroup) view.findViewById(R.id.group_radio_group);
        mRadioGroup.setOnCheckedChangeListener(this);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mGroupListMap = new HashMap<Integer,GroupListFragment>();
        mRadioGroup.check(R.id.group_all_btn);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction transaction=getChildFragmentManager().beginTransaction();
        if(mCurrFragment!=null)
            transaction.hide(mCurrFragment);
        switch (checkedId){
            case R.id.group_all_btn:
                if(mGroupListMap.get(0)==null){
                    GroupListFragment fragment=GroupListFragment.newInstance(0);
                    mGroupListMap.put(0,fragment);
                    transaction.add(R.id.fragment_container,fragment,"all");
                } else {
                    transaction.show(mGroupListMap.get(0));
                }
                mCurrFragment=mGroupListMap.get(0);
                break;
            case R.id.group_success_btn:
                if(mGroupListMap.get(1)==null){
                    GroupListFragment fragment=GroupListFragment.newInstance(1);
                    mGroupListMap.put(1,fragment);
                    transaction.add(R.id.fragment_container,fragment,"success");
                } else {
                    transaction.show(mGroupListMap.get(1));
                }
                mCurrFragment=mGroupListMap.get(1);
                break;
            case R.id.group_fail_btn:
                if(mGroupListMap.get(2)==null){
                    GroupListFragment fragment=GroupListFragment.newInstance(2);
                    mGroupListMap.put(2,fragment);
                    transaction.add(R.id.fragment_container,fragment,"fail");
                } else {
                    transaction.show(mGroupListMap.get(2));
                }
                mCurrFragment=mGroupListMap.get(2);
                break;
        }
        transaction.commitAllowingStateLoss();
    }
}
